package com.example.demo.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.ScAttendanceType;
import com.example.demo.mapper.ScAttendanceTypeMapper;
import com.example.demo.service.impl.ScAttendanceTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/attendance")
public class ScAttendanceTypeController {
    private static SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm");


    @Resource
    ScAttendanceTypeMapper scAttendanceTypeMapper;
    @Autowired
    ScAttendanceTypeServiceImpl scAttendanceTypeService;

    @PostMapping
    public Result<?> save(@RequestBody ScAttendanceType scAttendanceType) throws ParseException {
        if (StrUtil.isBlank(scAttendanceType.getWorkBegin()) || StrUtil.isBlank(scAttendanceType.getWorkEnd()))
            return Result.error("401", "缺少上下班时间！");
//        scAttendanceType.setDayWork(calculate(scAttendanceType.getWorkBegin(), scAttendanceType.getWorkEnd()));
//        scAttendanceType.setMonthWork2(scAttendanceType.getDayWork().multiply(scAttendanceType.getDays()));
//        BigDecimal rest = scAttendanceType.getRestWork().multiply(scAttendanceType.getDays());
//        BigDecimal work = scAttendanceType.getMonthWork2().subtract(rest);
//        scAttendanceType.setMonthWork1(scAttendanceType.getMonthWork2().subtract(rest));
        return Result.success(scAttendanceTypeService.saveOrUpdate(scAttendanceType));
    }

    @GetMapping("/list")
    public synchronized Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<ScAttendanceType> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(ScAttendanceType::getName, name);
        }
        Page<ScAttendanceType> scAttendanceTypePage = scAttendanceTypeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(scAttendanceTypePage);
    }

    public BigDecimal calculate(String date1, String date2) throws ParseException {
        Date date3 = dateFormat1.parse(date1);
        Date date4 = dateFormat1.parse(date2);
        long period = DateUtil.between(date3, date4, DateUnit.MINUTE);
        if (period <= 120) period = 1440;
        return new BigDecimal(period / 60.0);
    }
}
